package feizhou.untils.tools;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时区转换
 */
public class OutingTimeZoneTransDataUtil {


    /**
     * @param time
     * @return
     */
    public static Date getFirstSecondOfDay(Date time) {
        Calendar lastDate = Calendar.getInstance();
        lastDate.setTime(time);
        lastDate.set(Calendar.HOUR_OF_DAY, 0);
        lastDate.set(Calendar.SECOND, 0);
        lastDate.set(Calendar.MINUTE, 0);
        lastDate.set(Calendar.MILLISECOND, 0);
        return lastDate.getTime();
    }


    public static Date getLastSecondOfDay(Date date) {
        Calendar lastDate = Calendar.getInstance();
        lastDate.setTime(date);
        lastDate.set(Calendar.HOUR_OF_DAY, 23);
        lastDate.set(Calendar.SECOND, 59);
        lastDate.set(Calendar.MINUTE, 59);
        lastDate.set(Calendar.MILLISECOND, 0);
        return lastDate.getTime();
    }
    /**
     * 根据时间搓转换date入库
     * @param time
     * @param timeZone
     * @return
     */
    public static Date getDateByTimeZone(long time,String timeZone){
        if(timeZone==null || "".equals(timeZone)){
            timeZone= DateUtil.CHINA_TIMEZONE;
        }
        SimpleDateFormat londonSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        londonSdf.setTimeZone(TimeZone.getTimeZone(timeZone));
        SimpleDateFormat dateBase = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateBase.parse(londonSdf.format(new Date(time)));
        }catch (Exception e){

        }
        return null;
    }

    /**
     * 根据date转换成long
     * @param date
     * @param timeZone
     * @return
     */
    public static long getDateByTimeZone(Date date,String timeZone){
        if(timeZone==null || "".equals(timeZone)){
            timeZone= DateUtil.CHINA_TIMEZONE;
        }
        try {
            SimpleDateFormat dateBase = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
           return sdf.parse(dateBase.format(date)).getTime();
        }catch (Exception e){

        }
        return 0;
    }


    public static void main(String[] args) throws Exception{
        Long time=1564329600000L;
        //System.out.println(getDateByTimeZone(getDateByTimeZone(time,"GMT-5:00"),"GMT-5:00"));

        // System.out.println(new Date(.getTime()));

        // Date date=getLastSecondOfDay(getDateByTimeZone(time,"GMT-5:00"));
        //  Date date1=getFirstSecondOfDay(getDateByTimeZone(time,"GMT-5:00"));
        // long time1 =getDateByTimeZone(date,"GMT-5:00") ;

        //System.out.println(date);

        //System.out.println(getDateByTimeZone(time1,"GMT-5:00"));

        //"Tue Jul 30 12:00:00 CST 2019"

        //System.out.println(getDateByTimeZone(1564459200000L,"GMT-4:00"));
        Date date=getDateByTimeZone(1582992000000L,"GMT+8:00");
        System.out.println(date);

        //Tue Jul 30 04:00:00 CST 2019 1564416000000 1564416000000
    }
}
